<?php


namespace app\shop\controller\admin;


use app\shop\controller\BaseAdminController;
use app\shop\model\config\SensitiveWordModel;
use app\shop\model\config\SensitiveWordRecordModel;
use app\shop\model\config\TypePayModel;
use app\shop\model\config\TypeProgramModel;
use app\shop\model\user\AdminRoleModel;
use app\shop\model\user\MessageModel;
use app\shop\model\user\UserModel;
use think\App;

class SensitiveWordController extends BaseAdminController
{

    public function __construct(App $app)
    {
        $this->namespace = __NAMESPACE__;
        $this->model = new SensitiveWordModel();
        $this->search = [
            'like' => ['name']
        ];
        parent::__construct($app);
    }

    /**
     * 关键词列表
     * @return array|\think\response\Json
     */
    public function getList()
    {
        try {
            $list = parent::getList()->toArray();
            foreach ($list['data'] as &$v) {
                $v['count'] = MessageModel::where('sensitive_word_id', $v['id'])->count();
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 触发敏感词记录
     * @return \think\response\Json
     */
    public function getTriggerRecord()
    {
        try {
            $id = $this->request->param('id');
            $list = MessageModel::where('sensitive_word_id', $id)
                ->order($this->orderBy, $this->sort)
                ->paginate(['list_rows' => $this->limit, 'page' => $this->page])
                ->toArray();
            foreach ($list['data'] as &$v) {
                $v['count'] = MessageModel::where('sensitive_word_id', $v['sensitive_word_id'])->where('from_user', $v['from_user'])->count();
                $userInfo = UserModel::find($v['from_user']);
                $v['user_name'] = $userInfo['real_name'] ?? '';
                $roleId = $userInfo['role_id'] ?? 0;
                $v['role_name'] = $roleId ? AdminRoleModel::where('id', $roleId)->value('role_name') : '客户';
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 触发敏感词列表
     * @return \think\response\Json
     */
    public function getTriggerList()
    {
        try {
            $where = [];
            $where[] = ['sensitive_word_id', '>', 0];
            $search = $this->request->param('search', '', 'trim');
            if ($search) {
                $wordId = SensitiveWordModel::where('name', $search)->value('id');
                $where[] = ['sensitive_word_id', '=', $wordId];
            }
            $list = MessageModel::where($where)
                ->order('msg_id', 'desc')
                ->paginate(['list_rows' => $this->limit, 'page' => $this->page])
                ->toArray();
            foreach ($list['data'] as &$v) {
//                $v['count'] = MessageModel::where('sensitive_word_id', $v['sensitive_word_id'])->where('from_user', $v['from_user'])->count();
//                $v['name'] = SensitiveWordModel::where('id', $v['sensitive_word_id'])->value('name');
                $userInfo = UserModel::find($v['from_user']);
                $v['from_username'] = $userInfo['realname'] ?? '';
                $roleId = $userInfo['role_id'] ?? 0;
                $v['from_role_name'] = $roleId ? AdminRoleModel::where('id', $roleId)->value('role_name') : '客户';

                $userInfo1 = UserModel::find($v['to_user']);
                $v['to_username'] = $userInfo1['realname'] ?? '';
                $roleId1 = $userInfo1['role_id'] ?? 0;
                $v['to_role_name'] = $roleId1 ? AdminRoleModel::where('id', $roleId1)->value('role_name') : '客户';
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 触发敏感词聊天记录
     * @return \think\response\Json
     */
    public function getMsgRecord()
    {
        try {
            $id = $this->request->param('id', '', 'trim');
            $msgInfo = MessageModel::find($id);
            if (!$msgInfo) {
                throw new \Exception('消息不存在');
            }
            $firstMsgId = MessageModel::where('chat_identify', $msgInfo['chat_identify'])
                ->where('msg_id', '<', $id)
                ->limit(10)
                ->min('msg_id');
            $endMsgId = MessageModel::where('chat_identify', $msgInfo['chat_identify'])
                ->where('msg_id', '>', $id)
                ->limit(10)
                ->max('msg_id');
            $where = [];
            $where[] = ['chat_identify', '=', $msgInfo['chat_identify']];
            $where[] = ['msg_id', '>=', $firstMsgId];
            $where[] = ['msg_id', '<=', $endMsgId];
            $list = MessageModel::where($where)
                ->order('id', 'asc')
                ->paginate(['list_rows' => $this->limit, 'page' => $this->page])
                ->toArray();
            foreach ($list['data'] as &$v) {
                $userInfo = UserModel::find($v['from_user']);
                $v['from_username'] = $userInfo['realname'] ?? '';
                $userInfo = UserModel::find($v['to_user']);
                $v['to_username'] = $userInfo['realname'] ?? '';
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }
}